package com.bdqn.t382.controller;

import com.bdqn.t382.util.R;
import com.bdqn.t382.pojo.HotSpotConsultation;
import com.bdqn.t382.pojo.Information;
import com.bdqn.t382.pojo.Page;
import com.bdqn.t382.pojo.Reply;
import com.bdqn.t382.service.InformationService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 信息控制器
 */
@RestController
@RequestMapping("/information")
public class InformationController {

    private static final Logger logger = LoggerFactory.getLogger(InformationController.class);

    @Autowired
    private InformationService informationService;

    /**
     * 分页查询信息
     *
     * @param pageNo  当前页码
     * @param pageSize 每页大小
     * @return 分页结果
     */
    @RequestMapping("/page")
    public R getPage(@RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        if (pageNo == null || pageSize == null) {
            logger.warn("分页参数为空");
        }
        Page page = informationService.getPage(pageNo, pageSize);
        if (page == null || page.getList() == null || page.getList().isEmpty()) {
            logger.warn("未找到分页数据");
        }
        return R.ok(page);
    }

    /**
     * 查询热点咨询内容
     *
     * @param id 信息ID
     * @return 热点咨询列表
     */
    @PostMapping("/content")
    public R<List<HotSpotConsultation>> getContent(@RequestParam Integer id) {
        if (id == null) {
            logger.warn("信息ID为空");
        }
        List<HotSpotConsultation> hotSpotConsultation = informationService.getHotSpotConsultation(id);
        if (hotSpotConsultation == null || hotSpotConsultation.isEmpty()) {
            logger.warn("未找到热点咨询数据");
        }
        return R.ok(hotSpotConsultation);
    }

    /**
     * 添加回复
     *
     * @param reply 回复对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public R<Reply> add(@RequestBody Reply reply) {
        if (reply == null) {
            logger.warn("回复对象为空");
        }
        int num = informationService.insertReplies(reply);
        if (num <= 0) {
            logger.error("添加回复失败");
        }
        return R.ok(reply);
    }

    /**
     * 更新信息
     *
     * @param information 信息对象
     * @return 操作结果
     */
    @PostMapping("/update")
    public R<Information> update(@RequestBody Information information) {
        if (information == null) {
            logger.warn("信息对象为空");
        }
        int num = informationService.updateInformation(information);
        if (num <= 0) {
            logger.error("更新信息失败");
        }
        return R.ok(information);
    }

    /**
     * 删除信息及其回复
     *
     * @param id 信息ID
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public R<Integer> delete(@RequestParam Integer id) {
        if (id == null) {
            logger.warn("信息ID为空");
        }
        int num1 = informationService.deleteInformation(id);
        int num2 = informationService.deleteReplies(id);
        if (num1 <= 0 || num2 <= 0) {
            logger.error("删除信息或回复失败");
        }
        return R.ok(num1);
    }
}